<?php
/***************************************************************************
*                                register.php
 *
 *   Begin date           	: 
 *   Copyright            	: 
 *   Email                	: 
 ***************************************************************************/
defined('PHP5_PHP') or die("Application is stopping!!!");
if(!checkLoginUser()) {
	$msgAlert = $lang['_LOGIN_MSG_'];
	$redirect = sefBuild($php5WebPath, '', 1);
}
require_once($php5RootPath . "/classes/sms_group.class.php");
$row = new SMSGroup($php5DB);
@ini_set('memory_limit', '500M');
$task 		= php5GetParam($_REQUEST, 'task', '');
$type_id = $id 		= php5GetParam($_REQUEST, 'id', '');
$row->load($id);
if(!$row->name) {
	return;
}
/*if($id==1) {
	$type_id = 3;
} else if($id==2) {
	$type_id = 2;
} else if($id==3) {
	$type_id = 0;
} else if($id==4) {
	$type_id = 4;
} */
$smarty->setCompileDir(sprintf($php5RootTempalteCPath, ''));
$tpl = sprintf($php5TemplateFile, $language, 'sms_group', 'edit.tpl');
if(!is_file($tpl )) {
	die("can not find template");
}


//check task
$msg = '';
$noerr = 1;
switch($task)
{
	case 'update':
		$row->bind( $_POST );
		if(!trim($row->name)) {
			$noerr = 0;
			$msg= sprintf($lang['_BLANK_MSG_'], 'Name');
		}
		/////////////////////////check group
		$str = preg_replace( array("/,/", "/_/", "/%/"), array("\,", "\_", "\%"), $row->name);
		$query = "SELECT id "
			. "\n FROM #__sms_group "
			. "\n WHERE name LIKE '" . mysql_real_escape_string($str) . "' AND user_id = '".$php5Session->getVar("user_id")."' AND id != '$id' LIMIT 1"
			;
		$php5DB->setQuery( $query );
		$checkGroupId = intval($php5DB->loadResult());
		if($checkGroupId) {
			$noerr = 0;
			$msg = sprintf($lang['_EXIST_MSG_'], 'Name');
		}
				
		if($noerr) {

			$row->user_id = $php5Session->getVar("user_id");
			if (!$row->store()) {
				$msg = $msgAlert = "Error!!! Please try again!!!";		
			} else {
				$msg = $msgAlert = $lang['_UPDATE_MSG_'];
				$redirect = sefBuild($php5WebPath, 'index.php?o=sms_group&m=main', 1);
			}
		}
		break;
	default:
		break;	
		
}
$smarty->assign('msg', '');
if($msg) {
	$smarty->assign('msg', $msg);
}
//assign
$smarty->assign('name', $row->name);
$row->list = str_replace("\r\n","\n", $row->list);
$smarty->assign('arrLists', explode("\n",$row->list));
$smarty->assign('list', $row->list);
$smarty->assign('link_import', sefBuild($php5WebPath, 'index.php?o=import_newsletter_users', 1));
$smarty->assign('link_export', sefBuild($php5WebPath, 'index.php?o=export_newsletter_users', 1)."?&type=$type_id");
$smarty->assign('urlloaduser', $php5WebPath.'/load_user_email.php');
$smarty->assign('link_return', sefBuild($php5WebPath, 'index.php?o=sms_group&m=main', 1));
$smarty->assign('action', sefBuild($php5WebPath, 'index.php?o=sms_group&m=edit&task=update&id='.$id, 1));
$smarty->assign('main', $smarty->fetch($tpl));

return;
